<?php

namespace App\Http\Controllers\home;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
//use App\Http\Requests\UserPostRequest;//引入请求控制器
use DB;//引入数据库对象
use Hash;//引入加密算法

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //用户注册
    public function postInsert(Request $request)
    {
        // dd($request);
        //提取数据
        $data = $request->only(['id','name','repassword','password']);
       
        //验证两次密码是否相等
        if ($data['password']!==$data['repassword']) {
            return redirect('home/login/login')->with('error','两次密码不一致');
        }
        //排除数据,只拿入库数据
         $data = $request->except(['repassword','_token']);
        //进行密码加密
        $data['password'] = Hash::make($data['password']);

        //执行数据入库操作
        $res = DB::table('user')->insertGetId($data);
        
        //跳转到列表页
        if($res){
           // return back()->withInput();
             return redirect('/home/index');
            //return redirect()->intended('/home/index');
        }
        // }else{
        //     return back()->withInput();
        // }
    }

    //编辑用户资料  ajax方法
    public function getUserupdate(Request $request){
        // $id=session('id')
        //接收id和value的值
        $id=$request->input('id');
        $sex=$request->input('attr');
        //执行语句
        if ($sex) {
           $res=DB::table('userDetails')->where('id',$id)->update(['sex'=>$sex]);
        if($res){
            echo 1;
        }else{
            echo 2;
        }
        }
        
        //修改个人简介
        $int=$request->input('val');
        if ($int) {
            $res1=DB::table('userDetails')->where('id',$id)->update(['introduce'=>$int]);
         if($res1){
            echo 1;
        }else{
            echo 2;
        }

        }
        
        //修改居住地
        //修改个人简介
        $int1=$request->input('val1');
        if ($int1) {
            $res2=DB::table('userDetails')->where('id',$id)->update(['domicile'=>$int1]);
         if($res2){
            echo 1;
        }else{
            echo 2;
        }
        }

        //修改行业信息
         $int2=$request->input('val2');
        if ($int2) {
            $res2=DB::table('userDetails')->where('id',$id)->update(['industry'=>$int2]);
         if($res2){
            echo 1;
        }else{
            echo 2;
        }
        }

         //修改签名信息
         $int3=$request->input('val3');
        if ($int3) {
            $res2=DB::table('userDetails')->where('id',$id)->update(['autograph'=>$int3]);
         if($res2){
            echo 1;
        }else{
            echo 2;
        }
        }
       
    }
    //用户登录处理ajax
    public function getLogin(Request $request){
            //接收用户名
        $name=$request->input('name');
        //接收密码
        $password=$request->input('password');
        //查出数据
        $res=DB::table('user')->where('name',$name)->get();
        if (!$res) {
           echo 1;//用户名不正确
           die;
        }
        //取出密码
        $pwd=$res[0]->password;
       //密码匹对
        $res1=Hash::check($password,$pwd);
        if (!$res1) {
            echo 2;//密码错误
            die;
        }
        if($res && $res1){
            echo 3;//用户名和密码都对
        }
    }
    //处理登录的方法
     public function postLogin(Request $request){
        //接收用户名
        $name=$request->input('name');
        //查出数据
        $res=DB::table('user')->where('name',$name)->get();
        //获取id
        $id=$res[0]->id;

        if ($res) {    
            //查询用户详情表
            $res1=DB::table('userDetails')->where('uid',$id)->get();
            $nname=$res1[0]->nickName;
            $pic=$res1[0]->pic;
            //将id,用户名,昵称,图片写入到session中
            session_start();
            $_SESSION['id']=$id;
            $_SESSION['name']=$name;
            $_SESSION['nname']=$nname;  
            $_SESSION['pic']=$pic;  
        }
         return view('home.index',['uid'=>$id,'name'=>$name,'nname'=>$nname]);//把用户的id带过去
    }

    //查询用户的详细资料
    public function cxzl(Request $request){
        $uid=$request->input('id');
        //开始查询用户数据
        $data=DB::table('userDetails')->where('uid',$uid)->get();
        //查询用户的动态  
        $arr=[];
        //点赞答案关系表
        $da=DB::table('guanxida')->where('uid',$uid)->orderby('datime','desc')->get();
        foreach ($da as $k1 => $v) {
             $arr[$k1]['time']=$v->datime;
            //查询答案 需要的数据压入数组
            $hd=DB::table('answer')->where('id',$v->did)->get();
            foreach ($hd as $k => $v) {
                $arr[$k1]['content']=$v->content;
                 //查询问题表  需要的数据压入数组
                    $wt=DB::table('issue')->where('id',$v->Iid)->get();
                    //根据问题查出用户详情表 需要的数据压入数组
                    foreach ($wt as $k2 => $v) {
                        $arr[$k1]['atitle']=$v->atitle;
                        $ud=DB::table('userDetails')->where('uid',$v->uid)->get();
                       foreach ($ud as $k => $v) {
                        //把用户详情数据压入数组
                           $arr[$k1]['upic']=$v->pic;
                           $arr[$k1]['uname']=$v->nickName;
                           $arr[$k1]['yjh']=$v->introduce;
                       }
                        //把需要的数据压入数组中
                    }
            }
           
        }
        // ($arr);
        //问题关注关系表
        $arr1=[];
        $wt=DB::table('guanxiwt')->where('uuid',$uid)->orderby('wtime','desc')->get();
        foreach ($wt as $k1 => $v) {
            $arr1[$k1]['time']=$v->wtime;
            $wt=DB::table('issue')->where('id',$v->wid)->get();
            //根据关系表查出来问题标题
            foreach ($wt as $k => $v) {
                $arr1[$k1]['atitle']=$v->atitle;
            }
        }
        // var_dump($arr1);
        //赞文章关系表
        $arr2=[];
        $wz=DB::table('guanxiwz')->where('uuuid',$uid)->orderby('wztime','desc')->get();
        foreach ($wz as $k1 => $v) {
            $arr2[$k1]['time']=$v->wztime;
            //查出文章的内容
            $rw=DB::table('article')->where('id',$v->wzid)->get();
             
            foreach ($rw as $k => $v) {
               //根据文章查出用户信息来
                 $arr2[$k1]['content']=$v->content;
                 $arr2[$k1]['atitle']=$v->title;

                $ud=DB::table('userDetails')->where('uid',$v->uid)->get();
                     foreach ($ud as $k => $v) {
                       
                        //把用户详情数据压入数组
                           $arr2[$k1]['upic']=$v->pic;
                           $arr2[$k1]['uname']=$v->nickName;
                           $arr2[$k1]['yjh']=$v->introduce;
                       }
            }
        }
        //根据时间把三个数组合并到一个数组中
        $tarr[]=$arr;
        $tarr[]=$arr1;
        $tarr[]=$arr2;
        //var_dump(array_values($tarr));
        //三维数组转换成二维数组
        $array=[];
        foreach ($tarr as $k => $v) {
            foreach($v as $k=>$value){
                $array[]=$value;
            }
        }
        //按照时间进行排序
        $sort = array(
         'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
         'field'     => 'time',       //排序字段
         );
         $arrSort = array();
         foreach($array AS $uniqid => $row){
             foreach($row AS $key=>$value){
                 $arrSort[$key][$uniqid] = $value;
             }
         }
         if($sort['direction']){
             array_multisort($arrSort[$sort['field']], constant($sort['direction']), $array);
         }
        // $da=DB::table('guanxida')->where('uid',$uid)
        return view('home.user.userdata',['data'=>$data,'array'=>$array]);
    }

     //执行用户头像上传
    public function postPicup(Request $request)
    {

        //调用方法进行头像上传
        // $data['pic'] = $this->upload($request);
         $id=$request->input('id');
          //提取数据
        $data = $request->except(['_token']);
        //进行入库前的处理
        $pic = self::upload($request);
        //执行数据入库操作
        $res=DB::table('userDetails')->where('id',$id)->update(['pic'=>$pic]);
        //跳转到列表页
        if($res){
           echo "<script>alert('上传成功！');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
         }else{
            echo "<script>alert('上传失败!');window.location.href='{$_SERVER['HTTP_REFERER']}'</script>";
         }
    }
    //处理头像上传
    static public function upload($request)
    {
        //判断是否有文件上传
        if($request->hasFile('pic')){
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //获取文件的后缀名
            $suffix = $request->file('pic')->getClientOriginalExtension();
            $arr = ['png','jpeg','gif','jpg'];
            if(!in_array($suffix,$arr)){
                return back()->with('error','上传文件格式不正确');
            }
            $request->file('pic')->move('./uploads/', $name.'.'.$suffix);
            //返回路径
            return '/uploads/'.$name.'.'.$suffix;
        }
        }

  
}
